//
//  InputNameTextViewController.m
//  SenMo
//
//  Created by zengyuan on 16/2/20.
//  Copyright © 2016年 ZengYuan. All rights reserved.
//

#import "InputNameTextViewController.h"
#import "RegexKitLite.h"
#import "UserUnit.h"

@interface InputNameTextViewController ()

@end

@implementation InputNameTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.titleLabel.text=@"姓名";
}


-(void)navRightAction{
    
    if ([textField.text length]==0) {
        MBALERT(@"内容不能为空");
        return;
    }
    
    if([textField.text length]>10){
        MBALERT(@"姓名太长");
        return;
    }
    
    if(![textField.text isMatchedByRegex:@"^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$"]){
        MBALERT(@"不是正确的姓名格式");
        return;
    }
    
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    if(!sid)return;
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"session_id"]=sid;
    dic[@"user_name"]=textField.text;
    
    
    NSString *json  =[dic JSONString];
    NSString *url = [NSString stringWithFormat:MAINURL,@"set_user_info.action"];
    
    [AWHttpTool post:url body:json success:^(id json) {
        
        NSString *msg = HTTPERROR_DATAFAIL;
        
        if (json) {
            
            NSInteger sta = [json[@"status"] integerValue];
            msg = json[@"msg"];
            
            if (sta == 0) {
                UserUnit *user = [UserUnit getActiveUser];
                user.userName  = textField.text;
                [UserUnit resetLoginUnit:user];
                
                MBALERT(@"修改成功");
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                MBALERT(msg);
            }
        }
        
     } failure:^(NSError *error) {
         MBALERT(@"修改失败");

     }];
    
 
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
